<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>组件间传值</title>
  <script src="vue.js"></script>
</head>

<body>
  <div id="app">
    <input type="text" v-model="todoVal">
    <button @click="btnClick">提交</button>
    <ul>
      <todo-item :content="item" v-for="(item,index) in List" 
      @delete="handleItemDelete" :index="index"></todo-item>
    </ul>
  </div>

  <script>
    // 使用组件 改造TodoList
    // 通过子组件向父组件传值，实现点击item删除本身
    // 子组件向父组件传值，通过$.emit()方法触发事件
    // 然后在父组件下的该子组件中监听该事件
    var TodoItem = {
      props: ['content','index'],
      template: "<li @click='handleItemClick'>{{content}}</li>",
      methods: {
        handleItemClick: function() {
          // alert('click');
          this.$emit("delete", this.index); // 触发事件，传递参数
        }
      }
    }
    var app = new Vue({
      el: '#app',
      components: {
        TodoItem
      },
      data: {
        todoVal: '',
        List: []
      },
      methods: {
        btnClick: function () {
          this.List.push(this.todoVal);
          this.todoVal = ''
        },
        handleItemDelete: function(index) {
          // console.log('delete');
          this.List.splice(index, 1); // 从index下标开始，删除1项
        }
      }
    });

  </script>

</body>

</html>